﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace NuaTarDoneng.Tail
{
    /// <summary>
    /// Status 的摘要说明
    /// </summary>
    public class Status : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string result = "ER";
            string action = context.Request.QueryString["action"];
            string info = context.Request.QueryString["info"];
            if (null != context.Session["uid"])
            {
                Logs.Log(context, context.Session["uid"].ToString() + ":" + action);
            }
            else
            {
                Logs.Log(context, action);
            }
            if (null!=context.Session["uid"])
            {
                string uid = context.Session["uid"].ToString();
                string key = tailSql.getInfo(uid, "code");
                string ninfo = SecurityMan.Decodes(key, info);
                if (action == "getTailine")
                {
                    result = tailSql.getInfo(ninfo, "tline");
                }
                else if (action == "getPhoto")
                {
                    result = tailSql.getInfo(ninfo, "Photo");
                }
               
                else if (action == "getGPS")
                {
                    if (context.Application["Status"+ninfo] != null)
                    {
                        result = context.Application["Status" + ninfo].ToString();
                    }
                   
                }
                else if (action == "getOnlineF")
                {
                 string ons= context.Application["OnlineG"].ToString();
                 string[] fa = tailSql.getInfo(context.Session["uid"].ToString(), "friendList").Split(',');
                 foreach (string a in fa)
                 {
                     if (ons.IndexOf(a) >= 0)
                     {
                         result +=(","+a);
                     }
                 }
                 result = result.TrimStart(',');
                }
                else if (action == "getOnline")
                {
                    result = context.Application["OnlineG"].ToString();

                }
                else if (action == "getFriendlist")
                {
                    result = tailSql.getInfo(uid,"friendList");

                }
                else if (action == "AddFriend")
                { 
                    string fuid=ninfo;
                    string friendlist = tailSql.getInfo(uid, "friendList")+","+ninfo;
                    tailSql.UpdateInfo(uid, "friendList", friendlist.TrimStart(','));
                }
                else if(action=="DelFriend")
                {
                
                }
                else
                {
                    result = "ER";
                }
            }
            context.Response.Write(result);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}